﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.EntityFrameworkCore;
using Net5ApiAndEfMySql.DtoModel.Common;
using Net5ApiAndEfMySql.IRepository.SystemAdmin;
using Net5ApiAndEfMySql.PoModel.SystemAdmin;

namespace Net5ApiAndEfMySql.Repository.SystemAdmin
{
    public class RoleDbRepository : BaseRepository, IRoleDbRepository
    {
        private SoaSystemCenterDbContext _soaSystemCenterDbContext;
        public RoleDbRepository(SoaSystemCenterDbContext soaSystemCenterDbContext)
        {
            _soaSystemCenterDbContext = soaSystemCenterDbContext;
        }

        public List<RoleDbPo> GetRoleListByUserName(string userName)
        {
            //获取用户角色关系表的Queryable
            var aurps = _soaSystemCenterDbContext.AdminUserRoleRelationPo.AsQueryable();
            //获取角色表的Queryable
            var rdps = _soaSystemCenterDbContext.RoleDbPo.AsQueryable();
            //用linq实现查询对应用户名的角色的 Queryable
            var query = from aur in aurps
                        join
     rdp in rdps on aur.role_id equals rdp.role_id
                        where aur.user_name == userName
                        select rdp;
            //执行Queryable 查询数据库 返回数据
            List<RoleDbPo> roleList = query.ToList();
            return roleList;
        }
        public PagerResult<RoleDbPo> GetPageList(SearchModel m)
        {
            IQueryable<RoleDbPo> iq = _soaSystemCenterDbContext.RoleDbPo.AsQueryable();
            if (m.SearchItem != null && m.SearchItem.Count > 0)
            {
                var tempDi = m.SearchItem;
                string roleName = "role_name";
                if (tempDi.Keys.Contains(roleName) && !string.IsNullOrEmpty(tempDi[roleName]))
                {
                    iq = iq.Where(c => c.role_name.Contains(tempDi[roleName]));
                }
                string roleStatus = "role_status";
                if (tempDi.Keys.Contains(roleStatus) && !string.IsNullOrEmpty(tempDi[roleStatus]))
                {
                    iq = iq.Where(c => c.role_status==int.Parse(tempDi[roleStatus].ToString()));
                }
            }
            return Pager(iq, m);
        }

        
    }
}
